

package cuttings;


import java.io.*;
import java.net.*;
import java.util.* ;
import java.lang.Object.*;
import java.lang.Integer;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;


class TCPClient
{
    public static String name = "user1";
    String password = "123456";
    public static String ip = "0.1.2.3";
    public static String hname = "hostname";
    public static String email = "xxx@x.com";

    //PeersList peersList = new PeersList();
    //LinkedList peersList = new LinkedList();
    byte[] buffer = new byte[1024];
    int welcomeport = 7000;
    public static String server_return = "init";
    public static boolean iAmHost = false;

    public static String MyGameHostName = "NULL";
    public static String MyGameID = "NULL";


    public void setParameters(String uname,String pwd,String mail)
    {
        name = uname;
        password = pwd;
        email = mail;
    }

    public void getMyDetails() throws Exception
    {
       try
       {
            InetAddress myself = InetAddress.getLocalHost ();
            hname = myself.getHostName();
            ip = myself.getHostAddress();
            System.out.println("LocalIP:"+ip);
       }
       catch (UnknownHostException ex)
       {
            System.out.println(ex);
       }
    }

    public int establishConnection(int mode) throws Exception
    {
       getMyDetails();

       ServerConnect serverobj = new ServerConnect(name,password,welcomeport,this,email,mode);
       Thread serverthread = new Thread(serverobj);
       serverthread.start();
       
       //this.wait();
       

       while((PeerInfoList.getcondition()==0)&&(server_return.equals("init")))
           Thread.sleep(1);

       if(server_return.equals("invalid"))
       {
           System.out.println("invalid user pwd.");
           return 0;
       }

          //System.out.println("inwhile");
       System.out.println("getcondition clear");
       //String peersInfoList = connectToServer();
       //processPeersInfo(peersInfoList);

       createRecvSocket(); //opening a welcomesocket
       createPeerSockets();
       createListenThread(); //replace with SendSocketsList.sendToAll(sentence);
       return 1;
    }


    public void createRecvSocket() throws Exception
    {
        System.out.println("in recvsock");
        ServerSocket welcomeSocket = new ServerSocket(welcomeport);
        WelcomeConnection welcomeobj = new WelcomeConnection(welcomeSocket);
        Thread welcomethread = new Thread(welcomeobj);
        welcomethread.start();
    }


    public void createPeerSockets() throws Exception
    {
        System.out.println("in peersock");
        for(int i=0;i<PeerInfoList.peersList.size();i++)
        {
            //create sockets for each peer in list and spin a thread to listen to it
            PeerInfo temp = (PeerInfo) PeerInfoList.peersList.get(i);
            createSendThread(temp.hostAddress,temp.port,temp.name); //save the sendsocket
        }
    }

    public void createListenThread() throws Exception
    {
        System.out.println("in listenthread");
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        KeyboardTracker obj = new KeyboardTracker(inFromUser);
        Thread trackerthread = new Thread(obj);
        trackerthread.start();
    }

    /*
    public void createRecvThread(Socket socket)
    {
        ReceiveMsg request = new ReceiveMsg(socket);
        Thread recvThread = new Thread(request);
        recvThread.start();
    }
    */

    public void createSendThread(String userip,int port,String username) throws Exception
    {
       System.out.println("in cst:"+userip+","+port);
        Socket socket = new Socket(userip,port);
        System.out.println("socket created");
       SendMsg request = new SendMsg(socket,userip,port,username);
       Thread sendThread = new Thread(request);
       sendThread.start();
       //request.processRequest();
    }
   


}


